package me.luger.project.picc.controller.admin;

import me.luger.core.commom.util.StringUtil;
import me.luger.core.dao.nutz.entity.page.DataTableRequest;
import me.luger.core.dao.nutz.entity.page.DataTableResult;
import me.luger.core.mvc.controller.BaseController;
import me.luger.core.mvc.entity.Result;
import me.luger.core.mvc.interceptor.oplog.OpLog;
import me.luger.project.picc.entity.Insurance;
import me.luger.project.picc.service.InsuranceService;
import org.nutz.lang.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import me.luger.project.picc.service.PrizeListService;
import me.luger.project.picc.entity.PrizeList;

import java.util.List;

/**
 * 用户中奖列表 controller
 */
@Controller
@RequestMapping("/private/picc/prizeList")
public class PrizeListController extends BaseController {

    private String viewBasePath = "/admin/modules/prizeList/";

    @Autowired
    private PrizeListService prizeListService;

    @Autowired
    private InsuranceService insuranceService;

    @RequestMapping(value = "",name = "用户中奖列表列表页面")
    public ModelAndView index(){
        return btlView(viewBasePath + "index");
    }

    @RequestMapping(value = "data",name = "获取用户中奖列表列表")
    @ResponseBody
    public Object data(@RequestBody final DataTableRequest dataTableRequest){
        DataTableResult<PrizeList> prizeListDataTableResult = prizeListService.data(dataTableRequest);

        List<PrizeList> prizeLists = prizeListDataTableResult.getData();

        for(PrizeList prizeList : prizeLists){
            if(StringUtil.isBlank(prizeList.getUserName()) && prizeList.getTypeId() == 3){
                Insurance insurance = insuranceService.fetchById(prizeList.getBizId());
                prizeList.setUserName(insurance.getUserName());
            }
        }
        return prizeListDataTableResult;
    }



    @RequestMapping(value = "delete",name = "删除用户中奖列表接口")
    @ResponseBody
    public Object delete(String[] ids){
        prizeListService.delete(ids);
        return Result.success("delete success");
    }

    @RequestMapping(value = "/detail/{id}",name = "获取用户中奖列表详细信息接口")
    public ModelAndView detail(@PathVariable("id") Integer id){
        PrizeList prizeList = prizeListService.fetchById(id);
        setAttr("prizeList",prizeList);
        return btlView(viewBasePath + "detail");
    }

    @RequestMapping(value = "add",name = "添加用户中奖列表页面")
    public ModelAndView add(){
        return btlView(viewBasePath + "add");
    }

    @RequestMapping(value = "addDo",name = "添加用户中奖列表接口")
    @ResponseBody
    @OpLog(name = "添加用户中奖列表",msg = "添加用户中奖列表")
    public Object adddo(PrizeList prizeList){
        try{
            prizeList = prizeListService.insert(prizeList);
        }catch (Exception e){
            return Result.error();
        }
        return Result.success();
    }

    @RequestMapping(value = "/edit/{id}",name = "更新用户中奖列表信息页面")
    public ModelAndView edit(@PathVariable Integer id){
        PrizeList prizeList = prizeListService.fetchById(id);
        setAttr("prizeList",prizeList);
        return btlView(viewBasePath + "edit");
    }

    @RequestMapping(value = "editDo",name = "更新用户中奖列表接口")
    @ResponseBody
    public Object editDo(PrizeList prizeList){
        try{
            prizeListService.updateIgnoreNull(prizeList);
        }catch (Exception e){
            return Result.error();
        }
        return Result.success();
    }
}
